<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_roles extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();	
		
		require('engine/include_dao.php');
		return true;
		
	}
	public function create_role($data){
		//print_r($data);exit;
		$role = new Role();
		$role->first_name = $data['first_name'];
		$role->created_date = date("Y-m-d H:i:s");
		$role->updated_date = date("Y-m-d H:i:s");
		if($role_id=DAOFactory::getRolesDAO()->insert($role))
		{
			$module_ids = explode(',',$data['module_id']);
		foreach($module_ids as $module_id)
		{	
			$role = new Role();
			$role->role_id = $role_id;
			$role->module_id = $data['module_id'];
			$result = DAOFactory::getRolesDAO()->insert_modules($role);
			//print_r($result);exit;
			/* $query1 = "insert into `role_modules` (role_id,module_id,created_date,updated_date) VALUES ('".$lastId."','".$module_id."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
			$result=$this->conn->dbCommand($query1); */
		}
		}
	/* if($query="insert into `roles` (first_name,created_date,updated_date) VALUES ('".$data['first_name']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')")
	{
		$result=$this->conn->dbCommand($query);
		$lastId=$this->conn->dbLastId();
		$module_ids = explode(',',$data['module_id']);
		foreach($module_ids as $module_id)
		{	
			$query1 = "insert into `role_modules` (role_id,module_id,created_date,updated_date) VALUES ('".$lastId."','".$module_id."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
			$result=$this->conn->dbCommand($query1);
		} 
	}	*/
	if($result !== ''){
			return array('message'=>'Role successfully created');			
		}else{
			return array('message'=>'Role Creation Failed');
		}	
	}
	public function edit_role($data){
		
		$role = new Role();
		$role->first_name = $data['first_name'];
		$role->updated_date = date("Y-m-d H:i:s");
		$role->role_id = $data['role_id'];
		$role_id=DAOFactory::getRolesDAO()->update($role);
			//$query1 = DAOFactory::getRolesDAO()->delete_role_modules($role);
		
	 if($role_id !== 0)
	{
		//print_r($role_id);exit;
		$result = DAOFactory::getRolesDAO()->delete_role_modules($role);
	/* 	$query1 = "delete from role_modules where role_id=".$data['role_id'];
		$result=$this->conn->dbCommand($query1);
	 */	$module_ids = explode(',',$data['module_id']);
		foreach($module_ids as $module_id)
		{	
		$role->module_id = $data['module_id'];
		$result = DAOFactory::getRolesDAO()->insert_role_modules($role);
		/* $query1 = "insert into `role_modules` (role_id,module_id,created_date,updated_date) VALUES ('".$data['role_id']."','".$module_id."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
			$result=$this->conn->dbCommand($query1);  */
		} 	
		if($result !== ''){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}
		
		
	}
	}
	public function get_role($data)
	{
		$role = new Role();
		$role->role_id = $data['role_id'];
		$result = DAOFactory::getRolesDAO()->get_role($role);
		//print_r($result);exit;
	/* $query="select first_name,role_id from`roles` where role_id=".$data['role_id'];
	
		$result=$this->conn->dbMultiple($query); */		
		if($result !== ''){
			return array('message'=>'Role get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Role get Failed');
		}	
	
	
	}
	public function role_modules($data)
	{
		$role = new Role();
		$role->role_id = $data['role_id'];
		$result = DAOFactory::getRolesDAO()->get_role_modules($role);
		//print_r($result);exit;
	/* $query="SELECT module_id FROM role_modules WHERE role_id=".$data['role_id'];
		
		$result=$this->conn->dbMultiple($query); */
			
				
		if($result){
			return array('message'=>'Rolemodules get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Rolemodules get Failed');
		}
	
	
	}
	public function get_rolemodules_list()
	{
		$result = DAOFactory::getRolesDAO()->get_rolemodules_list();
		/* $query="select module_id,module_name from modules where parent_module_id > '0'";
		$result=$this->conn->dbMultiple($query); */
		//print_r(array_filter($result));exit;
		if($result){
			return array('message'=>'Rolemodules get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Rolemodules get Failed');
		}	
		
	}
	public function delete_role($data){
		$role = new Role();
		$role->role_id = $data['role_id'];
		$role->status = $data['status'];
		$result = DAOFactory::getRolesDAO()->delete_role($role);
	/* $query = "UPDATE roles LEFT JOIN role_modules ON roles.role_id=role_modules.role_id
	SET roles.status='".$data['status']."',role_modules.status='".$data['status']."' WHERE roles.role_id=".$data['role_id'];
		
		$data=$this->conn->dbCommand($query); */
		//print_r($result);exit;
		if($result){
			return array('message'=>"Role Deleted Successfully");		
		}else
			return array('message'=>'Role Deletion Fails');
	}
	public function all_roles($data){
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	
		$role = new Role();
		$role->order_by = $order_by;
		$role->sort_order = $sort_order;
		$role->results_per_page = $results_per_page;
		$role->offset = $offset;
		$result = DAOFactory::getRolesDAO()->all_roles($role);
	/* $query="select * from roles order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$result=$this->conn->dbMultiple($query); */
		if($result){
			$totalrows = DAOFactory::getRolesDAO()->total_roles();
			//print_r($totalrows);exit;
			//$this->conn->dbSingle('SELECT count(*) as total from roles');
		return array('message'=>"All ","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No roles');
	
	}
	
	
}
	
?>